﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace ProductWebApi
{
    /// <summary>
    /// 读取配置文件
    /// </summary>
    public class ConfigurationHelper
    {
        private static IConfiguration _config;
        public ConfigurationHelper(IConfiguration config)
        {
            _config = config;
        }

        /// <summary>
        /// 获取数据库的类型
        /// </summary>
        /// <returns></returns>
        public static DatabaseType GetDatabaseType()
        {
            var databaseTypeString = _config["ConnectionService:DatabaseType"];
            var databaseType = (DatabaseType)Enum.Parse(typeof(DatabaseType), databaseTypeString);
            return databaseType;
        }

        /// <summary>
        /// 获取SqlService数据库的连接字符串
        /// </summary>
        /// <returns></returns>
        public static string GetConnectionString()
        {
            var databaseType = GetDatabaseType();
            string connection;
            switch (databaseType)
            {
                case DatabaseType.SqlServer:
                    connection = _config["ConnectionService:ConnectionSqlService"];
                    break;
                case DatabaseType.Mysql:
                    connection = _config["ConnectionService:ConnectionMysql"];
                    break;
                default:
                    connection = _config["ConnectionService:ConnectionSqlService"];
                    break;
            }
            return connection;
        }

    }
    public enum DatabaseType
    {
        Mysql,
        SqlServer
    }
}
